/*
 * Copyright (C) 2005, Velocityme Partnership
 * GanttChartDTO.java
 *
 * Created on 23 October 2005, 12:17
 */

package com.velocityme.www.dto;

import com.velocityme.valueobjects.TaskValue;
import com.velocityme.valueobjects.TaskValueToString;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

/**
 *
 * @author  Robert
 */
public class GanttChartDTO implements java.io.Serializable {
    
    private Collection m_categories = new ArrayList();
    private Collection m_starts = new ArrayList();
    private Collection m_ends = new ArrayList();
    private Collection m_percents = new ArrayList();
    
    /** Creates a new instance of GanttChartDTO */
    public GanttChartDTO(javax.swing.tree.TreeModel p_taskTreeModel) {
        recursiveAddTask(p_taskTreeModel, p_taskTreeModel.getRoot(), 0);
    }
    
    private void recursiveAddTask(TreeModel p_taskTreeModel, Object p_task, int p_level) {
        Object userObject = ((DefaultMutableTreeNode)p_task).getUserObject();
        if (userObject instanceof TaskValue) {
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < p_level-1; i++)
                buffer.append(" - ");
            TaskValue taskValue = (TaskValue)userObject;
            buffer.append(TaskValueToString.toString(taskValue));
            m_categories.add(buffer.toString());
            m_starts.add(new Double(taskValue.getStartDate().getTime()));
            m_ends.add(new Double(taskValue.getStopDate().getTime()));
            Byte percent = taskValue.getPercentageComplete();
            if (percent != null)
                m_percents.add(new Double(percent.byteValue()/100.0));
            else
                m_percents.add(new Double(0.0));
        }
        for (int i = 0; i < p_taskTreeModel.getChildCount(p_task); i++) {
            Object nextTask = p_taskTreeModel.getChild(p_task, i);
            recursiveAddTask(p_taskTreeModel, nextTask, p_level+1);
        }
    }
    
    public Comparable[] getSeriesKeys() {
        return null;
    }
    
    public Comparable[] getCategoryKeys() {
        Comparable[] categoryKeys = new Comparable[m_categories.size()];
        Iterator i = m_categories.iterator();
        int j = 0;
        while(i.hasNext()) {
            String category = (String)i.next();
            categoryKeys[j++] = category;
        }
        return categoryKeys;
    }
    
    public Number[][] getStarts() {
        Number[][] starts = new Number[1][m_starts.size()];
        Iterator i = m_starts.iterator();
        int j = 0;
        while(i.hasNext()) {
            Double start = (Double)i.next();
            starts[0][j++] = start;
        }
        return starts;
    }
    
    public Number[][] getEnds() {
        Number[][] ends = new Number[1][m_ends.size()];
        Iterator i = m_ends.iterator();
        int j = 0;
        while(i.hasNext()) {
            Double end = (Double)i.next();
            ends[0][j++] = end;
        }
        return ends;
    }
    
    public Number[][] getPercents() {
        Number[][] percents = new Number[1][m_percents.size()];
        Iterator i = m_percents.iterator();
        int j = 0;
        while(i.hasNext()) {
            Double percent = (Double)i.next();
            percents[0][j++] = percent;
        }
        return percents;
    }
    
    public int getNumBars() {
        return m_categories.size();
    }
    
}
